package com.test.travelinformation.fragment;

import android.app.AlertDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.qmuiteam.qmui.widget.QMUILoadingView;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;
import com.qmuiteam.qmui.widget.grouplist.QMUICommonListItemView;
import com.qmuiteam.qmui.widget.grouplist.QMUIGroupListView;
import com.test.travelinformation.R;
import com.test.travelinformation.activity.CollectedActivity;
import com.test.travelinformation.activity.LoginActivity;
import com.test.travelinformation.activity.ReleaseActivity;
import com.test.travelinformation.utils.SPUtils;

import java.util.List;

import static android.content.Context.MODE_PRIVATE;
import static cn.bmob.v3.Bmob.getApplicationContext;


public class Myfragment extends Fragment {
    TextView name;
    ImageView image;
    QMUIGroupListView groupListView;
    View view;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.my_fragment, container, false);
        groupListView = view.findViewById(R.id.grouplistview);
        image = view.findViewById(R.id.image);
        name = view.findViewById(R.id.name);
        initView();
        return view;
    }

    private void initView() {
        String userimage = SPUtils.get(getActivity(), "userimage", "").toString();
        if (userimage.length() != 0) {
            Glide.with(getActivity())
                    .load(userimage)
                    .apply(RequestOptions.bitmapTransform(new CircleCrop()))
                    .into(image);
        } else {
            Glide.with(getActivity())
                    .load("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1588160497812&di=e40fcc0dad2d48f291366f6a049348fb&imgtype=0&src=http%3A%2F%2Fwww.jf258.com%2Fuploads%2F2014-08-14%2F091212676.jpg")
                    .apply(RequestOptions.bitmapTransform(new CircleCrop()))
                    .into(image);
        }
        String username = SPUtils.get(getActivity(), "username", "张三").toString();
        name.setText(username);
        qMUIGroupListView();
        image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String url = SPUtils.get(getActivity(), "", "").toString();
                if (url.length() != 0) {
                    fangda(url);
                } else {
                    Toast.makeText(getActivity(), "暂无头像", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    private void qMUIGroupListView() {
        QMUICommonListItemView itemWithDetail1 = groupListView.createItemView("已收藏");
        itemWithDetail1.setDetailText("已经收藏的旅游攻略");//默认文字在左边   描述文字在右边

        QMUICommonListItemView itemWithDetail2 = groupListView.createItemView("已发布");
        itemWithDetail2.setDetailText("你发布的攻略");//默认文字在左边   描述文字在右边

        QMUICommonListItemView itemWithDetail3 = groupListView.createItemView("功能介绍");
        itemWithDetail3.setDetailText("功能介绍");//默认文字在左边   描述文字在右边

        QMUICommonListItemView itemWithDetail4 = groupListView.createItemView("切换账号");
        itemWithDetail4.setDetailText("退出登录重新登录");//默认文字在左边   描述文字在右边

        QMUICommonListItemView itemWithDetail5 = groupListView.createItemView("退出");
        itemWithDetail5.setDetailText("关闭app并重新新登录");//默认文字在左边   描述文字在右边

        View.OnClickListener onClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (v instanceof QMUICommonListItemView) {
                    CharSequence text = ((QMUICommonListItemView) v).getText();
                    if (text.equals("已收藏")) {
                        startActivity(new Intent(getActivity(), CollectedActivity.class));
                    } else if (text.equals("已发布")) {
                        startActivity(new Intent(getActivity(), ReleaseActivity.class));
                    } else if (text.equals("功能介绍")) {
                        Toast.makeText(getActivity(), "功能介绍", Toast.LENGTH_SHORT).show();

                    } else if (text.equals("切换账号")) {
                        new QMUIDialog.MessageDialogBuilder(getActivity())
                                .setTitle("确认要退出当前账号吗?")
                                .addAction("取消", new QMUIDialogAction.ActionListener() {
                                    @Override
                                    public void onClick(QMUIDialog dialog, int index) {
                                        dialog.dismiss();
                                    }
                                })
                                .addAction("确认", new QMUIDialogAction.ActionListener() {
                                    @Override
                                    public void onClick(QMUIDialog dialog, int index) {
                                        SharedPreferences sp = getActivity().getSharedPreferences("share_data", MODE_PRIVATE);
                                        if (sp != null) {
                                            sp.edit().clear().commit();
                                        }
                                        startActivity(new Intent(getActivity(), LoginActivity.class));
                                    }
                                })
                                .create().show();
                    } else if (text.equals("退出")) {
                        new QMUIDialog.MessageDialogBuilder(getActivity())
                                .setMessage("退出后需重新登录")
                                .setTitle("确认要退出吗?")
                                .addAction("取消", new QMUIDialogAction.ActionListener() {
                                    @Override
                                    public void onClick(QMUIDialog dialog, int index) {
                                        dialog.dismiss();
                                    }
                                })
                                .addAction("确认", new QMUIDialogAction.ActionListener() {
                                    @Override
                                    public void onClick(QMUIDialog dialog, int index) {
                                        SharedPreferences sp = getActivity().getSharedPreferences("share_data", MODE_PRIVATE);
                                        if (sp != null) {
                                            sp.edit().clear().commit();
                                        }
                                        getActivity().finish();
                                        System.exit(0);
                                    }
                                })
                                .create().show();
                    }
                }
            }
        };//默认文字在左边   自定义加载框按钮

        QMUIGroupListView.newSection(getContext())
                .addItemView(itemWithDetail1, onClickListener)
                .addItemView(itemWithDetail2, onClickListener)
                .addItemView(itemWithDetail3, onClickListener)
                .addItemView(itemWithDetail4, onClickListener)
                .addItemView(itemWithDetail5, onClickListener)
                .addTo(groupListView);
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

    }

    private void fangda(String url) {
        LayoutInflater inflater = LayoutInflater.from(getActivity());
        View imgEntryView = inflater.inflate(R.layout.public_dialog_image, null);
        final AlertDialog dialog = new AlertDialog.Builder(getActivity()).create();
        ImageView imageView = imgEntryView.findViewById(R.id.large_image);
        Glide.with(getActivity())
                .load(url)
                .into(imageView);
        dialog.setView(imgEntryView);
        dialog.getWindow().setBackgroundDrawableResource(R.color.transparent);
        dialog.show();
        imgEntryView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
    }
}